home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / ORBIT_SO / EASYDIAL.C next >
Text File  |  1992-07-19  |  7KB  |  300 lines

  1. /* Dialog handler routines.  Quick and easy */
  2. /* Copyright 1987                            */
  3. /* David Palmer                             */
  4. /* Mail code 220-47                            */
  5. /* California Institute Of Technology         */
  6. /* Uses EasyDialog.c  (also ⌐ 1987 By David Palmer) */
  7. /* Duplication, modification, and examination allowed on a    */
  8. /* non-commercial basis only.  Commercial use prohibited    */
  9. /* without prior written agreement with the author.  (This    */
  10. /* includes sale by for-profit companies, and use as an        */
  11. /* inducement to buy something.)                            */
  12.  
  13. /*        This package handles modal dialogs in a very easy to program    */
  14. /* way.  Objects in a dialog box either change data, cause actions,        */
  15. /* or both.  What they do is controlled by a data structure passed,        */
  16. /* along with the dialog's resource id, to the dialog handler.            */
  17. /*         The format of the data structure is described in the headerfile */
  18. /* EasyDialog.h                                                            */
  19.  
  20. #include <DialogMgr.h>
  21. #include <unix.h>
  22. #include "LSC Stars:EasyDialog.h"
  23. #include <StdFilePkg.h>
  24.  
  25. char *CtoPstr();
  26. char *PtoCstr();
  27.  
  28. int atoi();
  29. long atol();
  30. double atof();
  31.  
  32. char *Pto2Cstr(pchS, pchD)
  33. char *pchS, *pchD;
  34. {
  35.     int i;
  36.     char *pch;
  37.     pch = pchD;
  38.     for (i = *pchS++ ; i > 0 ; i--)
  39.         *pch++ = *pchS++;
  40.     return pchD;
  41. }
  42.     
  43.  
  44. char *Cto2Pstr(pchS, pchD)
  45. char *pchS, *pchD;
  46. {
  47.     char *pch;
  48.     for (pch = pchD + 1 ; *pchS != '\0' ; )
  49.         *pch++ = *pchS++;
  50.     *pchD = pch - pchD - 1;
  51.     return pchD;
  52. }
  53.  
  54. RetrieveValue(pdi, pedi)
  55. EDITEM *pedi;
  56. DialogPtr pdi;
  57. {
  58.     int cch;
  59.     char *pch, *pch2;
  60.     char rgch[MAXEDCCH];
  61.     int i;
  62.     long type;
  63.     Handle item;
  64.     Rect box;
  65.     
  66.     for (i = 0 ; i < pedi->nitems ; i++) {
  67.         GetDItem(pdi, i + pedi->firstitem, &type, &item, &box);
  68.         switch (pedi->itemtype) {
  69.             case edbutton:
  70.                 break;
  71.             case edchar:
  72.                 GetIText(item, rgch);
  73.                 ((char *)(pedi->pvalue))[i] = rgch[1];
  74.                 break;
  75.             case edstring:
  76.                 GetIText(item, rgch);
  77.                 Pto2Cstr(rgch, ((char *)pedi->pvalue) + i*MAXEDCCH);
  78.                 break;
  79.             case edint:
  80.                 GetIText(item, rgch);
  81.                 PtoCstr(rgch);
  82.                 stcd_i(rgch, ((int *)pedi->pvalue) + i);
  83.                 break;
  84.             case edfloat:
  85.                 GetIText(item, rgch);
  86.                 PtoCstr(rgch);
  87.                 sscanf(rgch, "%lg", ((double *)pedi->pvalue)+i);
  88.                 break;
  89.             case edrad:
  90.                  if (0 != GetCtlValue(item))
  91.                      *(int *)pedi->pvalue = i + 1;
  92.                 break;
  93.             case edcheck:
  94.                 ((int *)pedi->pvalue)[i] = GetCtlValue(item);
  95.                 break;
  96.             default:
  97.                 SysBeep(10);
  98.         }
  99.     }
  100. }
  101.  
  102. ShowValue(pdi, pedi)
  103. DialogPtr pdi;
  104. EDITEM *pedi;
  105. {
  106.     int cch;
  107.     char *pch, *pch2;
  108.     char rgch[MAXEDCCH];
  109.     int i;
  110.     long type;
  111.     Handle item;
  112.     Rect box;
  113.     
  114.     for (i = 0 ; i < pedi->nitems ; i++) {
  115.         GetDItem(pdi, i + pedi->firstitem, &type, &item, &box);
  116.         switch (pedi->itemtype) {
  117.             case edbutton:        /* surrounds default button */
  118.                 /* to be written */
  119.                 break;
  120.             case edchar:
  121.                 rgch[0] = '\001';
  122.                 rgch[1] = ((char *)(pedi->pvalue))[i];
  123.                 SetIText(item, rgch);
  124.                 break;
  125.             case edstring:
  126.                 SetIText(item,
  127.                     Cto2Pstr(((char *)pedi->pvalue) + i*MAXEDCCH, rgch));
  128.                 break;
  129.             case edint:
  130.                 rgch[0] = stci_d(&rgch[1], ((int *)pedi->pvalue)[i], 100);
  131.                 SetIText(item, rgch);
  132.                 break;
  133.             case edfloat:
  134.                 sprintf(rgch, "%g", ((double *)pedi->pvalue)[i]);
  135.                 SetIText(item, CtoPstr(rgch));
  136.                 break;
  137.             case edrad:
  138.                 SetCtlValue(item, Bool10((i + 1) == *(int *)pedi->pvalue));
  139.                 break;
  140.             case edcheck:
  141.                 SetCtlValue(item, Bool10(((int *)pedi->pvalue)[i]));
  142.                 break;
  143.             default:
  144.                 SysBeep(10);
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150. ChangeRCValue(pdi, pedi, itemnum)    /* change the value of an radio button or check box */
  151. DialogPtr pdi;
  152. EDITEM *pedi;
  153. int itemnum;
  154. {
  155.     if (pedi->itemtype == edrad) {
  156.         *(int *)pedi->pvalue = itemnum - pedi->firstitem + 1;
  157.         ShowValue(pdi, pedi);
  158.     } else if (pedi->itemtype == edcheck) {
  159.         *((int *)pedi->pvalue + itemnum - pedi->firstitem) =
  160.             ! *((int *)pedi->pvalue + itemnum - pedi->firstitem);
  161.         ShowValue(pdi, pedi);
  162.     } else {
  163.         SysBeep(3);
  164.     }
  165. }
  166.  
  167.  
  168. EDITEM *FindDiHit(pdi, pedi, itemnum)
  169. DialogPtr pdi;
  170. EDITEM *pedi;
  171. int itemnum;
  172. {
  173.     int i;
  174.     for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
  175.         if (pedi[i].firstitem <= itemnum &&
  176.                 itemnum < pedi[i].firstitem + pedi[i].nitems)
  177.             return &pedi[i];
  178.     }
  179.     return 0;
  180. }
  181.  
  182. int DoDiHit(pdi, pedi, itemnum)
  183. DialogPtr pdi;
  184. EDITEM *pedi;
  185. int itemnum;
  186. {
  187.     int returnvalue;
  188.     EDITEM *pedihit;
  189.     pedihit = FindDiHit(pdi, pedi, itemnum);
  190.     if (pedihit == 0) {
  191.         SysBeep(10);
  192.         return 0;
  193.     }
  194.     
  195.     switch (pedihit->itemtype) {
  196.         case edbutton:            /* buttons are ordinarily returned */
  197.                                 /* unless the button runs a function */
  198.             returnvalue = itemnum;
  199.             break;
  200.         case edrad:
  201.         case edcheck:
  202.             ChangeRCValue(pdi, pedihit, itemnum);
  203.             returnvalue = 0;
  204.             break;
  205.         case edchar:
  206.         case edstring:
  207.         case edint:
  208.         case edfloat:
  209.             returnvalue = 0;
  210.             break;
  211.         default:
  212.             SysBeep(5);
  213.             returnvalue = 0;
  214.     }
  215.     if (pedihit->pfunc != NULL) {
  216.         returnvalue = (*(pedihit->pfunc))(pdi, pedi, pedihit, itemnum);
  217.     }
  218.     return returnvalue;
  219. }
  220.     
  221.  
  222. int EasyDialog(id, pedi)
  223. int id;                                /* dialog resource number*/
  224. EDITEM *pedi;                        /* list of dialog items    */
  225. {
  226.     int i;
  227.     DialogPtr pdi;
  228.     int itemhit;
  229.     int whichbutton;
  230.     
  231.     pdi = GetNewDialog(id, NULL, -1l);
  232.     if (pdi == NULL) {
  233.         SysBeep(30);
  234.         return 0;
  235.     }
  236.     for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
  237.         if (pedi[i].finit) {
  238.             ShowValue(pdi, &pedi[i]);
  239.         }
  240.     }
  241.     if ( ((DialogPeek)pdi)->editField != -1) {
  242.         TESetSelect(0l, 32767l, ((DialogPeek)pdi)->textH);
  243.     }
  244.     do {
  245.         ModalDialog(NULL, &itemhit);
  246.     } while (0 == (whichbutton = DoDiHit(pdi, pedi, itemhit)));
  247.     for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
  248.         RetrieveValue(pdi, &pedi[i]);
  249.     }
  250.     DisposDialog(pdi);
  251.     return whichbutton;
  252. }
  253.  
  254. GetFile(pchtypes, pchfname, pvol)        /* Get volume and filename info */
  255. char *pchtypes, *pchfname;
  256. int *pvol;
  257. {
  258.     int trash;
  259.     int ntypes;
  260.     Point where;
  261.     SFReply sfr;
  262.     
  263.     where.h = 75; where.v = 75;
  264.     ntypes = strlen(pchtypes)/4;
  265.     if (ntypes == 0)
  266.         ntypes = -1;
  267.     SFGetFile(where, NULL, NULL, ntypes, pchtypes, NULL, &sfr);
  268.     if (sfr.good) {
  269.         if (pvol != 0)
  270.             *pvol = sfr.vRefNum;
  271.         SetVol("", sfr.vRefNum);            /* Set up volume for next open */
  272.         Pto2Cstr(sfr.fName, pchfname);
  273.         return TRUE;
  274.     } else
  275.         return FALSE;
  276. }
  277.  
  278. PutFile(pchoname, pchnname, pvol)
  279. char *pchoname, *pchnname;    /* old name, new name */
  280. int *pvol;                    /* volume number */
  281. {
  282.     Point where;
  283.     SFReply sfr;
  284.     
  285.     where.h = 75; where.v = 75;
  286.     if (pvol != 0 && *pvol != -1) {
  287.         sfr.vRefNum = *pvol;
  288.     } else {
  289.         GetVol(&sfr.fName, &sfr.vRefNum);
  290.     }
  291.     Cto2Pstr(pchoname, sfr.fName);
  292.     SFPutFile(where, "", &sfr.fName,  NULL, &sfr);
  293.     if (sfr.good) {
  294.         if (pvol != NULL)
  295.             *pvol = sfr.vRefNum;
  296.         Pto2Cstr(sfr.fName, pchnname);
  297.         return TRUE;
  298.     } else
  299.         return FALSE;
  300. }